import Axios from './axios'

class Api extends Axios {
  // 获取验证码
  async getCaptchaCode() {
    try {
      let result = await this.post({url: '/v1/captchas'})
      if (result.status === 1 && (result instanceof Object)) {
        return result || []
      } else {
        return {
          tip: '获取验证码失败',
          response: result,
        }
      }
    } catch (e) {
      throw e
    }
  }

  // 账号密码登录
  async accountLogin(data) {
    try {
      let result = await this.post({url: '/v2/login', data})
      if (result.status !== 0) {
        return result || []
      } else {
        return {
          tip: '登录失败',
          response: result,
        }
      }
    } catch (err) {
      throw err
    }
  }

  // 猜测当前定位城市
  async cityGuess(keyword) {
    try {
      let result = await this.post({url: '/v1/cities?type=guess'});
      if (result) {
        return result;
      } else {
        return {
          tip: '城市失败',
          response: result,
          url: '//elm.cangdu.org/v1/carts/addresses',
        };
      }
    } catch (err) {
      throw err;
    }
  }

  // 获取商店详情
  async getShopDetail(id, params) {
    try {
      let result = await this.get({url: `/shopping/restaurant/${id}`, params});
      if (result) {
        return result;
      } else {
        return {
          tip: '获取商店详情失败',
          response: result,
          url: '//elm.cangdu.org/v1/carts/addresses',
        };
      }
    } catch (err) {
      throw err;
    }
  }

  // 获取商店菜单
  async getFoodMenu(params) {
    try {
      let result = await this.get({url: '/shopping/v2/menu/', params});
      if (result) {
        return result;
      } else {
        return {
          tip: '获取商店详情失败',
          response: result,
          url: '//elm.cangdu.org/v1/carts/addresses',
        };
      }
    } catch (err) {
      throw err;
    }
  }

  // 获取商品种类
  async getFoodTypes(data) {
    try {
      //  + getUrlConcat(data)
      let result = await this.get({url: '/v2/index_entry/'});
      if (result) {
        return result;
      } else {
        return {
          tip: '获取商品种类失败',
          response: result,
          url: '//elm.cangdu.org/v1/carts/addresses',
        };
      }
    } catch (err) {
      throw err;
    }
  }

  // 获取商店列表
  // async getShopList( data){
  //   try{
  //     let result = await this.get('/shopping/restaurants/' + getUrlConcat(data));
  //     if(result){
  //       return result;
  //     }else{
  //       let err = {
  //         tip: '获取商店列表失败',
  //         response: result,
  //         url: '//elm.cangdu.org/v1/carts/addresses',
  //       }
  //       throw err;
  //     }
  //   }catch(err){
  //     throw err;
  //   }
  // }


}

export default new Api();